import React, {
    PureComponent
} from 'react';

import {
    Actions
} from 'react-native-router-flux';

import {
    Tip,
    Dialog,
    Socket
} from './../../components';

import Match from './../../components/Match';

import Message from './../../views/home/message';

import connect from './../../utils/connect';

import chatAction from './../../state/actions/chat';

import * as chatTypes from './../../state/types/chat';

import userAction from './../../state/actions/user';

import * as userTypes from './../../state/types/user';

interface Props {
    user: any,
    chatActions: any,
    userActions: any
}

interface ConnectionMessage {
    userId: string,
    message?: string,
    mesg?: string
}

@connect([`user`], {
    chatActions: chatAction,
    userActions: userAction
})
class Listen extends PureComponent <Props> {

    static defaultProps={
        user:{},
        chatActions:{},
        userActions:{}
    }

    get chatActions() {
        const {
            chatActions
        } = this.props;
        return chatActions;
    }

    get userActions() {
        const {
            userActions
        } = this.props;
        return userActions;
    }

    get loginUser() {
        const {
            user
        } = this.props;
        const {
            loginUser = {}
        } = user;
        return loginUser;
    }

    get userId() {
        const {
            userId = 0
        } = this.loginUser;
        return ~~userId;
    }

    componentDidMount() {
        //监听聊天消息
        Socket.on(
            Socket.CONST.chat,
            this.onListenChat
        )
        //监听强制用户下线
        Socket.on(
            Socket.CONST.logout,
            this.onListenLoginOut
        )
        //监听notice消息
        Socket.on(
            Socket.CONST.notice,
            this.onListenNotice
        )
        //监听用户访问
        Socket.on(
            Socket.CONST.visitor,
            this.onListenConnection
        )
        //监听用户喜欢
        Socket.on(
            Socket.CONST.likesYou,
            this.onListenConnection
        )
        //监听相互喜欢
        Socket.on(
            Socket.CONST.mutualLike,
            this.onListenMutualLike
        )
        //监听profile更新
        Socket.on(
            Socket.CONST.updatedUserInfo,
            this.onListenUpdatedUserInfo
        )
        //监听支付成功
        Socket.on(
            Socket.CONST.upgradeSuccessful,
            this.onListenUpdatedUserInfo
        )
    }

    private toChat = (item: any) => {
        const {
            fromUser
        } = item;

        const {
            userId,
            displayName
        } = fromUser;

        Actions.push(`/message`, {
            userId: userId,
            nickName: displayName
        })
    }

    private chatTip = (item: any) => {
        const {
            text,
            fromUser,
            chatRoomId
        } = item;

        const {
            userId,
            displayName
        } = fromUser;

        const inRoom = (
            this.checkInRoom(~~userId)
        )

        const _onPress = () => {
            this.toChat(item);
        }

        if (!inRoom) {
            const _message = (
                `${displayName}: ${text}`
            )
            Tip.success({
                onPress: _onPress,
                message: _message
            })
            const _para = {
                number: 1,
                chatRoomId: chatRoomId
            }
            this.chatActions[
                chatTypes.updateMessageUnread
            ](_para)
        }
    }

    private checkInRoom = (userId: number) => {
        const {
            currentScene
        } = Actions;
        if (currentScene !== `/message`) {
            return false
        }
        return ~~Message.curChatUserId === userId;
    }

    private onListenChat = (messages = []) => {
        messages.forEach(this.chatTip);
        const _para = { messages: messages };
        this.chatActions[chatTypes.appendMessageList](_para);
        this.chatActions[chatTypes.updateMessageList](_para);
    }

    private onListenLoginOut = () => {
        this.userActions[userTypes.userLoginOut];
    }

    private onListenNotice = (messages: Array<any>) => {
        messages.forEach(msg => {
            Dialog.alert({
                title: `Notice`,
                message: msg.text
            })
        })
    }

    private onListenConnection = (
        res: ConnectionMessage
    ) => {
        const {
            userId,
            mesg,
            message
        } = res;
        Tip.success({
            autoClose: false,
            message: message || mesg || ``,
            onPress: () => {
                Actions.push(`/profile/:userId`, {
                    userId: userId
                })
            }
        })
    }

    private onListenMutualLike = (e:any) => {
        const _content = (
            <Match
                users={e.users}
                userId={this.userId}
            />
        )
        Dialog.show(_content, {
            maskOpacity: .8,
            direction:'center'
        })
    }

    private onListenUpdatedUserInfo = () => {
        this.userActions[userTypes.loginUser]();
    }

    render() {
        return null;
    }
}

export default Listen;